<?php

/**
 * flexlib Scheduling Framework
 *
 */
class sfSchedulingUtil
{
	
	private $data = array();
	
	private $times = array();
	
	private $config = array(
	 'width'=>600,
	 'height'=>300,
	 'labelWidth'=>180,
	 'url'=>null,
	 'startDate'=>null,
	 'endDate'=>null,
	 'title'=>'sfSchedulingPlugin'
	
	);
	
	private $document = array(
	 'width'=>850,
	 'height'=>500,
	 'bgcolor'=>'#869ca7'
	);
	
	
	/**
	 * add time bar
	 *
	 * @param string $start 
	 * @param string $end
	 * @param string $label
	 * @param string $color
	 */
	public function addTime($start, $end, $label, $color = '0x999999', $uniqueKey = null)
	{
		
		if($uniqueKey === null)
		{
			
			$uniqueKey = count($this->times);
		}
		
		
		$this->times[] = array(
		  'start' => $start,
		  'end' => $end,
		  'labelText' => $label,
		  'color' => $color,
		  'uniqueKey' => $uniqueKey
		);
	}
	
	/**
	 * row data
	 *
	 * @param string $title
	 */
	public function addRow($title)
	{
		
		$this->data[] = array(
		  'title'=>$title,
		  'times' => $this->times
		);
		
		$this->times = array();
	}
	
	public function getData()
	{
		
		return $this->data;
	}
	
	
	/**
	 * document setting
	 *
	 * @param array $document
	 */
	public function setDocument($document = array())
	{
		
		$this->document = array_merge($this->document, $document);
	}
	
	/**
	 * configuration setting
	 *
	 * @param array $config
	 */
	public function setConfig($config = array())
	{
	 	
		if(isset($config['url']))
		{
			
			$config['url'] = url_for($config['url']);
			
		}
		
		$this->config = array_merge($this->config, $config);
	}
	
	
	/**
	 * publish timeline object
	 *
	 */
	public function publish()
	{
		$config = Zend_Json::encode($this->config);
		
		foreach($this->document as $key => $val)
		{

			$$key = $val;
			
		}
		
		$request = sfContext::getInstance()->getRequest();
		/* @var $request sfWebRequest */
		$filepath = sfConfig::get('sf_scheduling_item_path'
		  , $request->getRelativeUrlRoot() . "/sfSchedulingPlugin/timeline.swf");
		  
		require(dirname(__FILE__) . "/../../templates/object.php");
	}
	
	
}

